from layout import Layout
from unit import Unit
from battle import Battle
from db import DB
from item import UseItem
import urllib.parse
import config



def battle(monster_id, params):
    p = {k.replace(urllib.parse.quote("hero["), "").replace(urllib.parse.quote("]"), ""): urllib.parse.unquote(v) for k, v in params.items()}
    del p["monster"]
    if p["skill"]:
        p["skill"] = [int(i) for i in urllib.parse.unquote(p["skill"]).split(",")]
    if p["equipment"]:
        p["equipment"] = [int(i) for i in urllib.parse.unquote(p["equipment"]).split(",")]

    player = Unit(**p)
    monster = Unit(**DB().get_monster(monster_id))
    battle_info = Battle(player, monster)
    send = get_header("html") + battle_info.return_data().encode()

    return send

def useItem(item_id, params):
    p = {k.replace(urllib.parse.quote("hero["), "").replace(urllib.parse.quote("]"), ""): urllib.parse.unquote(v) for k, v in params.items()}
    del p["item"]
    if p["skill"]:
        p["skill"] = [int(i) for i in urllib.parse.unquote(p["skill"]).split(",")]
    if p["equipment"]:
        p["equipment"] = [int(i) for i in urllib.parse.unquote(p["equipment"]).split(",")]

    player = Unit(**p)
    info = UseItem(player).return_data(item_id)
    send = get_header("html") + info.encode()

    return send

def sorry():
    send = get_header("html") + "Sorry".encode()
    return send